/******************************************************************************
* collect event study results and generate tables
*****************************************************************************/


*load cut version to get varlist
use "${dataRoot}/tu_mover_delta${ext}_cut.dta", clear
labelVars 0 0

describe



global varFmt "%12.${tabDcPt}fc"
global mvRegY = string(${mvRegQ}/4, "%2.0f")


*********************************************************************
//organize event study regression estimates and generate a summary table
// Table 4, A4, A6
*********************************************************************

*compared mover individual change in outcome vs delta
clear
gen out1=""
save "${outDataDir}/event_estimates${ver}${ext}", replace


if $evt_sample_version==0{
	*restrict to 4 years pre and 7 years post move (Longer post period)
	global ver "_m47"
	global mvRegQ 24 
}
else{
	global ver ""
	global mvRegQ 16
}


*for each mover variables
local ct=1
foreach var in $keyMvVarsShow{
	
	
	* load mover estiamtes
	use "${outDataDir}/event_plot_sub16${ver}_`var'_czse${ext}.dta", clear
		
	*keep estimates at specific snapshot post move
	gen rel_q_str = rel_q
	destring rel_q, replace
	
	*get rel_q of quarter prior to move
	sum rel_q if stderr==0 & estimate==0
	*get relative quarter to move
	replace rel_q = rel_q-`r(mean)'-1
	keep if rel_q==${mvRegQ}
	rename rel_q rel_q_num
	rename rel_q_str rel_q

	*string for output tables
	qui gen out1 = string(estimate, "${varFmt}")
	qui gen out2 = "("+string(stderr, "${varFmt}")+")"
	qui drop estimate - max95


	* merge results with positive moves
	merge 1:m rel_q using "${outDataDir}/event_plot_sub16${ver}pos_`var'_czse${ext}.dta",  keepusing(estimate stderr)  keep(3)   nogenerate
	
	qui gen out_pos1 = string(estimate, "${varFmt}")
	qui gen out_pos2 = "("+string(stderr, "${varFmt}")+")"
	qui drop estimate stderr 
	
	* merge results with negative moves
	merge 1:m rel_q using "${outDataDir}/event_plot_sub16${ver}neg_`var'_czse${ext}.dta",  keepusing(estimate stderr)  keep(3)   nogenerate

	qui gen out_neg1 = string(estimate, "${varFmt}")
	qui gen out_neg2 = "("+string(stderr, "${varFmt}")+")"
	qui drop estimate stderr 

	
	* merge results with moves at young (<40)
	merge 1:m rel_q using "${outDataDir}/event_plot_sub16${ver}y40_`var'_czse${ext}.dta", keepusing(estimate stderr)  keep(3)   nogenerate

	qui gen out_young1 = string(estimate, "${varFmt}")
	qui gen out_young2 = "("+string(stderr, "${varFmt}")+")"
	qui drop estimate stderr 
	
	* merge results with moves at old (>=40)
	merge 1:m rel_q using "${outDataDir}/event_plot_sub16${ver}o40_`var'_czse${ext}.dta",  keepusing(estimate stderr)  keep(3)   nogenerate

	qui gen out_old1 = string(estimate, "${varFmt}")
	qui gen out_old2 = "("+string(stderr, "${varFmt}")+")"
	qui drop estimate stderr 
	

	* merge results with origin X event time FE
	merge 1:m rel_q using "${outDataDir}/event_plot_sub16${ver}ori_`var'_czse${ext}.dta", keepusing(estimate stderr)  keep(3)   nogenerate

	qui gen out_ori1 = string(estimate, "${varFmt}")
	qui gen out_ori2 = "("+string(stderr, "${varFmt}")+")"
	qui drop estimate stderr 
	
	
	* merge results with destination X event time FE
	merge 1:m rel_q using "${outDataDir}/event_plot_sub16${ver}des_`var'_czse${ext}.dta",  keepusing(estimate stderr)  keep(3)   nogenerate

	qui gen out_des1 = string(estimate, "${varFmt}")
	qui gen out_des2 = "("+string(stderr, "${varFmt}")+")"
	qui drop estimate stderr 
	

	* merge results at county level
	merge 1:m rel_q using "${outDataDir}/event_plot_sub16${ver}_`var'_czse_cntynodraw.dta", keepusing(estimate stderr)  keep(3)   nogenerate

	qui gen out_cnty1 = string(estimate, "${varFmt}")
	qui gen out_cnty2 = "("+string(stderr, "${varFmt}")+")"
	qui drop estimate stderr 
		
	
	* merge results at zip X age level
	merge 1:m rel_q using "${outDataDir}/event_plot_sub16${ver}_`var'_czse_zipnodrawage.dta",  keepusing(estimate stderr)  keep(3)   nogenerate

	qui gen out_zipage1 = string(estimate, "${varFmt}")
	qui gen out_zipage2 = "("+string(stderr, "${varFmt}")+")"
	qui drop estimate stderr 		
	
	

	*name the variable
	rename parm `var'
	labelVars 0 0
	qui replace `var' = "\addlinespace[0.1em] \\ " + "`:var l `var''" 
	rename `var' parm
	
	qui gen varOrder = `ct'
	append using "${outDataDir}/event_estimates${ver}${ext}"
	save "${outDataDir}/event_estimates${ver}${ext}", replace
	
	local ct=`ct'+1	
}




*reshape to the format of output table
sort varOrder 
drop rel_q*

reshape long out out_pos out_neg out_young out_old out_ori out_des out_samest out_crosst out_cnty out_zipage, i(varOrder) j(stats)
replace parm = ""  if stats ==2
drop varOrder stats
	
list

*** produce two version of event study result tables


*** 1) robust to subsamples
local texHead = "\begin{tabular}{ l c c c c c} \hline\hline  " + ///
				"   & \multicolumn{5}{c}{Fraction converged to outcome} \\ " + ///
				"    & \multicolumn{5}{c}{at destination ${mvRegY} years post move} \\ \cline{2-6} \\ " + ///
				"    &  (1)  &  (2) & (3) & (4) & (5)  \\ " + /// 
				" Financial distress measures   & \phantom{a}All movers \phantom{aa}&  Positive moves & Negative moves &   \phantom{a}Move <40  \phantom{aa} &  \phantom{a}Move $\geq 40$  \phantom{aa} \\ \hline " 

listtex parm out out_pos out_neg out_young out_old using "${outDir}/Tex/mover_event_study_estimates_q${mvRegQ}${ver}czse${ext}.tex" ,replace ///
		rstyle(tabular)	 head("`texHead'") ///
		foot("\addlinespace[0.2em] \\ \hline\hline \end{tabular}")		

*** 2) robust to different delta and origin/destination controls
local texHead = "\begin{tabular}{ l c c c c c} \hline\hline  " + ///
				"   & \multicolumn{5}{c}{Fraction converged to outcome} \\ " + ///
				"    & \multicolumn{5}{c}{at destination ${mvRegY} years post move} \\ \cline{2-6} \\ " + ///
				"    &  (1)  &  (2) & (3) & (4) & (5)  \\ " + /// 
				" Financial distress measures   & \phantom{a}All movers \phantom{aa} &  Same origin & Same destination &  Zip $\times$ age level &  County level \phantom{a} \\ \hline " 

listtex parm out out_ori out_des out_zipage out_cnty using "${outDir}/Tex/mover_event_study_estimates_q${mvRegQ}${ver}czse${ext}_appdx.tex" ,replace ///	
		rstyle(tabular)	 head("`texHead'") ///
		foot("\addlinespace[0.2em] \\ \hline\hline \end{tabular}")	
		
		
*time stamp
display "DONE!! $S_TIME  $S_DATE"
